<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>$Perl6 = $class.bless(:Example<12>);</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Perl6_ES11.html">class Examples11 is Perl6 { ... }</a>&nbsp;&nbsp;>>Next: <a href="Perl6_ES13.html">role Example13 { # Perl6 }</a></p>
<h1>$Perl6 = $class.bless(:Example<12>);</h1>
<div class='content'>
<p>Category: <a href='Perl6.html'>Perl6</a> &nbsp; Keywords: <b>class Perl6</b></p><h3>Pause</h3>
I'm sorry. pugs 还有很多没有实现，所以我也不知道写下来的是否正确，所以就先等等了。<br />
本章只是占个地方。可以略过不看。等 pugs 实现了 bless 等我会慢慢加进来。

<h3>构建和初始化</h3>
所有的类都从 Object 类继承了一个构建函数 new. 它能将传递进来的参数（只能是带名参数）分别初始化对应的属性。
<pre><code>Class Dog {
    has $.name;
    ...

my $pet = Dog.new(:name&lt;Eric>); # 这里的 new 继承自 Object, 而传递进来的 :name&lt;Eirc> 将初始化 $.name
</code></pre>
你可以自己写 new 来覆盖 Object 的 new. <br />
或者写你喜欢名字的构建器。与 Perl 5 中一样，一个构件器就是调用了 bless 的例程，不同的是类中调用的是方法。<br />
因为 pugs 还没实现 bless, 所以我也试不出来。只能暂时缓缓。

<h3>submethod BUILD</h3>
<pre><code>class Dog {
    has $.name;
    has $.mum;
    has $:age;
    
    submethod BUILD ($.name) {
        $.name ~= ' Lam';
    }
}
my $pet = Dog.new( :name&lt;Snoopy>, :mum&lt;Kitty>, :age&lt;2> );
say $pet.name; # Snoopy Lam</code></pre>
前面讲过 submethod 与 method 不同的是 submethod 是不会被继承的。<br />
这里的 BUILD 方法覆盖了 Object 里的方法。函数 bless 会通过调用 BUILDALL 来自动调用合适的 BUILD<br />

<h3>clone</h3>
<pre><code>class Dog {
    has $.name;
    has $.mum;
    has $:age;
    ...
}
my $pet = Dog.new( :name&lt;Snoopy>, :mum&lt;Kitty>, :age&lt;2> );
...
my $pet2 = $pet.clone(:name&lt;Lily>);
# 除了名字改为 Lily, 其 mum, age 都跟 $pet 一样，而且可以调用 $pet 能调用的东西</code></pre></div>
<p><&lt;Previous: <a href="Perl6_ES11.html">class Examples11 is Perl6 { ... }</a>&nbsp;&nbsp;>>Next: <a href="Perl6_ES13.html">role Example13 { # Perl6 }</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=$Perl6%20=%20$class.bless(:Example%3C12%3E);&url=http://www.fayland.org/journal/Perl6_ES12.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='050330.html'>Class::DBI 改 Primary 项</a> < <span class='digit'>2005-03-30 22:27:07</span> ></li><li><a href='Class-Accessor.html'>$Class[0] = 'Class::Accessor';</a> < <span class='digit'>2005-04-26 15:07:30</span> ></li><li><a href='Class-Data-Inheritable.html'>$Class[1] = 'Class::Data::Inheritable';</a> < <span class='digit'>2005-04-27 17:57:39</span> ></li><li><a href='Class-Trigger.html'>$Class[2] = 'Class::Trigger';</a> < <span class='digit'>2005-04-28 16:07:05</span> ></li><li><a href='Perl6_ES11.html'>class Examples11 is Perl6 { ... }</a> < <span class='digit'>2005-05-30 13:31:31</span> ></li><li><a href='Class_Date.html'>Class::Date 的两个常见用途</a> < <span class='digit'>2005-06-01 15:02:47</span> ></li></ul>
Created on <span class="digit">2005-05-31 11:38:46</span>, Last modified on <span class="digit">2005-05-31 13:07:22</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>